capture log close
log using $log/AED_dataprep_06_entries_ue, text replace
   
clear
set mem 5000m
set matsize 800
set more off

set trace off
   
/* ----------------------------------------- */
/* --- Define entries into unemployment ---- */
/* ----------------------------------------- */
/* ----------------------------------------- */



/* ----------------------------------------------------------------------- */
/* ------ Restrictions: -------------------------------------------------- */
/* --- * Entries by calendar year (2001 - 2008) -------------------------- */
/* --- * Number of months in employment before entry into unemployment --- */
/* --- * Allow multiple entries per calendar year ------------------------ */
/* ----------------------------------------------------------------------- */




foreach iebversion in v901 {

/*=== All entries into employment (erw_stat == 31 AND alo_dau == 0) ===*/

use $orig/AED_9310_v1, clear

rename persnr iza_id
rename begorig beg_orig
rename endorig end_orig
rename erwstat erw_stat


keep iza_id beg_orig end_orig erw_stat quelle 

sort iza_id beg_orig

drop if beg_orig < d(1jan2001) | beg_orig > d(31dec2010)

keep if erw_stat == 31 

sort iza_id beg_orig

tempfile medium
save `medium'

/*Up to 43 entries into unemployment per person per year*/
forvalue n = 1/43 {
	
	use `medium', clear
	
	by iza_id: keep if _n == `n'
	
	drop end_orig quelle erw_stat 

	rename beg_orig iza_eintrittd_`n'
	
	gen iza_eintritty_`n' = .
	
	gen iza_eintrittm_`n' = .
	
	forvalue year = 1/9 {
		replace iza_eintritty_`n' = 200`year' if iza_eintrittd_`n' <= d(31dec200`year') & iza_eintrittd_`n'>= d(1jan200`year')
		replace iza_eintrittm_`n' = 1 if iza_eintrittd_`n' <= d(31jan200`year') & iza_eintrittd_`n'>= d(1jan200`year')
		replace iza_eintrittm_`n' = 2 if iza_eintrittd_`n' <= d(28feb200`year') & iza_eintrittd_`n'>= d(1feb200`year')
		replace iza_eintrittm_`n' = 3 if iza_eintrittd_`n' <= d(31mar200`year') & iza_eintrittd_`n'>= d(1mar200`year')
		replace iza_eintrittm_`n' = 4 if iza_eintrittd_`n' <= d(30apr200`year') & iza_eintrittd_`n'>= d(1apr200`year')
		replace iza_eintrittm_`n' = 5 if iza_eintrittd_`n' <= d(31may200`year') & iza_eintrittd_`n'>= d(1may200`year')
		replace iza_eintrittm_`n' = 6 if iza_eintrittd_`n' <= d(30jun200`year') & iza_eintrittd_`n'>= d(1jun200`year')
		replace iza_eintrittm_`n' = 7 if iza_eintrittd_`n' <= d(31jul200`year') & iza_eintrittd_`n'>= d(1jul200`year')
		replace iza_eintrittm_`n' = 8 if iza_eintrittd_`n' <= d(31aug200`year') & iza_eintrittd_`n'>= d(1aug200`year')
		replace iza_eintrittm_`n' = 9 if iza_eintrittd_`n' <= d(30sep200`year') & iza_eintrittd_`n'>= d(1sep200`year')
		replace iza_eintrittm_`n' = 10 if iza_eintrittd_`n' <= d(31oct200`year') & iza_eintrittd_`n'>= d(1oct200`year')
		replace iza_eintrittm_`n' = 11 if iza_eintrittd_`n' <= d(30nov200`year') & iza_eintrittd_`n'>= d(1nov200`year')
		replace iza_eintrittm_`n' = 12 if iza_eintrittd_`n' <= d(31dec200`year') & iza_eintrittd_`n'>= d(1dec200`year')
	}
	
	
	replace iza_eintritty_`n' = 2010 if iza_eintrittd_`n' <= d(31dec2010) & iza_eintrittd_`n'>= d(1jan2010)
		replace iza_eintrittm_`n' = 1 if iza_eintrittd_`n' <= d(31jan2010) & iza_eintrittd_`n'>= d(1jan2010)
		replace iza_eintrittm_`n' = 2 if iza_eintrittd_`n' <= d(28feb2010) & iza_eintrittd_`n'>= d(1feb2010)
		replace iza_eintrittm_`n' = 3 if iza_eintrittd_`n' <= d(31mar2010) & iza_eintrittd_`n'>= d(1mar2010)
		replace iza_eintrittm_`n' = 4 if iza_eintrittd_`n' <= d(30apr2010) & iza_eintrittd_`n'>= d(1apr2010)
		replace iza_eintrittm_`n' = 5 if iza_eintrittd_`n' <= d(31may2010) & iza_eintrittd_`n'>= d(1may2010)
		replace iza_eintrittm_`n' = 6 if iza_eintrittd_`n' <= d(30jun2010) & iza_eintrittd_`n'>= d(1jun2010)
		replace iza_eintrittm_`n' = 7 if iza_eintrittd_`n' <= d(31jul2010) & iza_eintrittd_`n'>= d(1jul2010)
		replace iza_eintrittm_`n' = 8 if iza_eintrittd_`n' <= d(31aug2010) & iza_eintrittd_`n'>= d(1aug2010)
		replace iza_eintrittm_`n' = 9 if iza_eintrittd_`n' <= d(30sep2010) & iza_eintrittd_`n'>= d(1sep2010)
		replace iza_eintrittm_`n' = 10 if iza_eintrittd_`n' <= d(31oct2010) & iza_eintrittd_`n'>= d(1oct2010)
		replace iza_eintrittm_`n' = 11 if iza_eintrittd_`n' <= d(30nov2010) & iza_eintrittd_`n'>= d(1nov2010)
		replace iza_eintrittm_`n' = 12 if iza_eintrittd_`n' <= d(31dec2010) & iza_eintrittd_`n'>= d(1dec2010)
	
	
	replace iza_eintrittm_`n' = 2 if iza_eintrittd_`n' == d(29feb2004) | iza_eintrittd_`n' == d(29feb2008) 
	/*2004 and 2008 are leap years*/
	
	sort iza_id
	
	tempfile alo_`n'
	save `alo_`n'', replace
}

use `alo_1', clear
sort iza_id

forvalue x = 2/43 {
joinby iza_id using `alo_`x'', unmatched(master)
tab _merge
drop _merge
sort iza_id
}

gen iza_anz_alo_eintritte = 1
forvalue y = 2/43 {
replace iza_anz_alo_eintritte = `y' if iza_eintrittd_`y' ~= .
}

compress

/*Save this data because it contains all entries into UE, maybe needed later*/
save $data/anz_alo_eintritte_`iebversion', replace


use $data/anz_alo_eintritte_`iebversion', clear

drop iza_anz_alo_eintritte
keep iza_id iza_eintritty_1-iza_eintritty_43 iza_eintrittm_1-iza_eintrittm_43 iza_eintrittd_1-iza_eintrittd_43
reshape long iza_eintritty_ iza_eintrittm_ iza_eintrittd_, i(iza_id) j(lfd)

keep if iza_eintrittd_ ~= .
rename iza_eintrittd_ iza_eintrittd
rename iza_eintritty_ iza_eintritty
rename iza_eintrittm_ iza_eintrittm
tempfile all_entries
save `all_entries', replace


/*=== Generate a variable containing the number of months in employment during the last 12 months before entry into UE ===*/


forvalue year = 2001/2004 {

		forvalue month = 1/12 {
	
			use `all_entries', clear
			
			
			forvalue x = 1/9 {
			replace iza_eintritty = 200`x' if iza_eintrittd <= d(31dec200`x') & iza_eintrittd>= d(1jan200`x')
			}
			keep if iza_eintritty == `year' & iza_eintrittm == `month'
		
					
			sort iza_id iza_eintrittd
			by iza_id: drop if _n>1
			drop lfd
			
			local yearbefore = `year'-1
			local what "`yearbefore'/`year'"
			
			forvalue jahr = `what' {
			
			/*Merge outcomes variables*/
			joinby iza_id using $data/outcome_monthly_y`jahr'_`iebversion', unmatched(master)
			drop _merge
			
			/*Merge ALMP outcome variables*/
			joinby iza_id using $data/outcome_mth_y`jahr'_`iebversion', unmatched(master)
			drop _merge
			
			}
			
			keep iza_id iza_eintritt* iza_beh_* iza_leh_* iza_asu_* iza_mth1_*
			
			reshape long iza_beh_ iza_leh_ iza_asu_ iza_mth1_, i(iza_id) j(lfd)
			
			gen eintritt = .
			replace eintritt = `year'0`month' if `month'<10
			replace eintritt = `year'`month' if `month'>=10
			
			sort iza_id lfd
			
			by iza_id: drop if lfd>=eintritt
			
			/*Keep last 12 months before entry into UE*/
			by iza_id: gen N = _N
			by iza_id: gen n = _n
			by iza_id: replace N = N - 12
			by iza_id: keep if n > N
			
			
			/*Define dependent employment*/
			#delimit;
			gen sv = 1 if (iza_beh_==101 |
								iza_beh_==102 |
								iza_beh_==103 | 
								iza_beh_==105 | 
								iza_beh_==106 |
								iza_beh_==112 | 
								iza_beh_==118 | 
								iza_beh_==119 | 
								iza_beh_==120 | 
								iza_beh_==140 |
								iza_beh_==141 | 
								iza_beh_==142 | 
								iza_beh_==143 | 
								iza_beh_==149 | 
								iza_beh_==201 | 
								iza_beh_==203 |
								iza_beh_==205 )
								& (iza_asu_~=31 & iza_asu_~=32 & iza_leh_==. & iza_mth1_==.);
			
			sort iza_id lfd;
			by iza_id: replace sv=1 if sv[_n-1]==1 & sv==. & _n==_N;
			by iza_id: replace sv=1 if sv[_n-1]==1 & sv[_n+1]==1 & sv==. & _n>1 
								& (iza_asu_~=31 & iza_asu_~=32 & iza_leh_==. & iza_mth1_==.);
			#delimit cr
			
			/*Number of months in employment*/
			gsort iza_id -lfd
			by iza_id: gen sv_vor_alo0 = sv if _n==1
			by iza_id: replace sv_vor_alo0 = sv+sv_vor_alo0[_n-1] if sv==1 & sv_vor_alo0[_n-1]~=. & _n>1
			sort iza_id lfd
			by iza_id: egen sv_vor_alo = max(sv_vor_alo0)
			replace sv_vor_alo = 0 if sv_vor_alo==.
					
			by iza_id: drop if _n>1
			
			keep iza_id iza_eintritt* sv_vor_alo
			
			tempfile entries_`year'`month'
			save `entries_`year'`month'', replace
			
		}/*month*/
	
	/*Create yearly dataset*/
	use `entries_`year'1', clear
	forvalue month = 2/12 {
		append using `entries_`year'`month''
	}
		

	save $data/entries_ue_`year'_all_`iebversion', replace
	
	use $data/entries_ue_`year'_all_`iebversion',clear
		
			
	/*Random draw of ONE entry into UE per person */
	sort iza_id
	set seed 123456789
	by iza_id: gen random = uniform()
	sort iza_id random
	by iza_id: drop if _n > 1
	drop random
		
	keep iza_id iza_eintrittd iza_eintritty iza_eintrittm sv_vor_alo
	order iza_id iza_eintrittd iza_eintritty iza_eintrittm sv_vor_alo
		
	/*Only keep entries into UE with at least 3 months of employment before*/
	keep if sv_vor_alo>=3
		
		
	saveold $data/eintritte_ue_`year'_`iebversion', replace
	
	
}/*year*/

}

log close
